package ua.kiev.tosik.price.modules.search.gui.models;

import ua.kiev.tosik.price.gui.models.SimpleTableModel;
import ua.kiev.tosik.price.models.Prices;
import ua.kiev.tosik.price.modules.search.conf.ModuleConfig;

import javax.swing.*;
import java.util.Date;
import java.util.Vector;

public class PricesTableModel extends SimpleTableModel {

	private Vector<Prices> data;
	private ImageIcon priceIcon, selectedIcon;
	private ModuleConfig config;

	public PricesTableModel(Vector<Prices> data) {
		this.data = data;
		config = ModuleConfig.getInstance();
		priceIcon = new ImageIcon(config.get("search.price.table.record.icon"));
		selectedIcon = new ImageIcon(config.get("search.price.table.record.selected.icon"));
		colNames = new String[] {
			"[..]",
			config.get("search.price.table.filed.clientsName"),
			config.get("search.price.table.field.articleInternal"),
			config.get("search.price.table.field.article"),
			config.get("search.price.table.field.name"),
			config.get("search.price.table.field.price"),
			config.get("search.price.table.field.pricePercent"),
			config.get("search.price.table.field.date"),
			config.get("search.price.table.field.existence")
		};
	}

	@Override
	public int getRowCount() {
		return data.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Prices prices = data.get(rowIndex);
		switch (columnIndex) {
			case 0:
				return (prices.isSelected())?selectedIcon:priceIcon;
			case 1:
				return prices.getClientsName();
			case 2:
				return prices.getArticleInternal();
			case 3:
				return prices.getArticle();
			case 4:
				return prices.getName();
			case 5:
				return prices.getPrice();
			case 6:
				return prices.getPricePercent();
			case 7:
				return prices.getPricesDate(); // dateFormat.format(prices.getPricesDate());
			case 8:
				return prices.getExistence();
			default:
				return "";
		}
	}

	@Override
	public Class getColumnClass(int c) {
		switch (c) {
			case 0:
				return (ImageIcon.class);
			case 5:
				return (Double.class);
			case 6:
				return (Double.class);
			case 7:
				return (Date.class);
			default:
				return (String.class);
		}
	}

	public void removeRow(int idx) {
		data.remove(idx);
		fireTableRowsDeleted(idx, idx);
	}

	public void addRowSet(Vector<Prices> data) {
		this.data.clear();
		this.data.addAll(data);
		fireTableDataChanged();
		logger.debug("Added " + this.data.size() + " records to [Prices] table");
	}

	public void addRow(Prices rowData) {
		this.data.add(rowData);
		fireTableDataChanged();
	}

	public void addRow(int rowIndex, Prices rowData) {
		this.data.add(rowIndex, rowData);
		fireTableRowsInserted(rowIndex, rowIndex);
	}

	public void updateRow(int rowIndex, Prices rowData) {
		this.data.set(rowIndex, rowData);
		fireTableRowsUpdated(rowIndex, rowIndex);
	}

	public Prices getRow(int index) {
		return data.get(index);
	}


}
